import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/controllers/base/base_sports_controller.dart';
import 'package:share_fit/app/tools/date_tool.dart';
import 'package:share_fit/app/ute/uteped_const.dart';

import 'state.dart';

class GoalSettingLogic extends BaseSportsController {
  final GoalSettingState state = GoalSettingState();

  String selectedDurationText = 'selectedDurationText';
  String selectedDistanceText = 'selectedDistanceText';
  String selectedCalorieText = 'selectedCalorieText';

  late TabController tabController = TabController(length: state.tabTitleList.length, vsync: this);

  goBack() {
    Get.back();
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathSports;
  }

  @override
  void initState() {}

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }

  @override
  void onClose() {
    super.onClose();
    tabController.dispose();
  }

  changeCalorie(val) {
    state.selectedCalorie = val.toString();
    state.selectedDistance = '0';
    state.selectedDuration = '0';
    update([selectedCalorieText]);
  }

  changeDistance(val) {
    state.selectedDistance = val.toString();
    state.selectedDuration = '0';
    state.selectedCalorie = '0';
    update([selectedDistanceText]);
  }

  changeDuration(val) {
    state.selectedDuration = val.toString();
    state.selectedDistance = '0';
    state.selectedCalorie = '0';
    update([selectedDurationText]);
  }

  calorieCustomize(String value) {
    int v = int.parse(value.isEmpty ? '0' : value);
    if (v >= 10 && v <= 5000) {
      state.selectedCalorie = value;
      state.selectedDistance = '0';
      state.selectedDuration = '0';
      state.canSaveCalories = true;
    } else {
      state.canSaveCalories = false;
    }
    update(['canSaveCalories']);
  }

  distanceCustomize(String value) {
    double v = double.parse(value.isEmpty ? '0.0' : value);
    if (v >= 0.1 && v <= 100) {
      state.selectedDistance = value;
      state.selectedDuration = '0';
      state.selectedCalorie = '0';
      state.canSaveDistance = true;
    } else {
      state.canSaveDistance = false;
    }
    update(['canSaveDistance']);
  }

  durationCustomize(String value) {
    int v = int.parse(value.isEmpty ? '0' : value);
    if (v >= 1 && v <= 1440) {
      state.selectedDuration = value;
      state.selectedDistance = '0';
      state.selectedCalorie = '0';
      state.canSaveDuration = true;
    } else {
      state.canSaveDuration = false;
    }
    update(['canSaveDuration']);
  }

  startSports() {
    uTEPedSports.start(
      workoutType: state.sportType,
      // duration: int.parse(state.selectedDuration),
      duration: DateTool.hMS2Seconds(state.selectedDuration),
      distance: double.parse(state.selectedDistance),
      calories: int.parse(state.selectedCalorie),
    );
  }

  applyCustomize() {
    update([selectedCalorieText, selectedDurationText, selectedDistanceText]);
    Get.back();
    state.canSaveDuration = false;
    state.canSaveDistance = false;
    state.canSaveCalories = false;
    update(['canSaveDuration', 'canSaveCalories', 'canSaveDistance']);
  }

  onTab(int value) {
    /// 充值状态，因为时长、路程、卡路里都是使用这个状态
    state.showDurationWarning = false;
    if (value == 0) {
      state.selectedDuration = state.durationList[0]['label'].toString();
      state.selectedDistance = '0';
      state.selectedCalorie = '0';
      update([selectedDurationText]);
    } else if (value == 1) {
      state.selectedDistance = state.distanceList[0]['value'].toString();
      state.selectedDuration = '0';
      state.selectedCalorie = '0';
      update([selectedDistanceText]);
    } else if (value == 2) {
      state.selectedCalorie = state.calorieList[0]['value'].toString();
      state.selectedDistance = '0';
      state.selectedDuration = '0';
      update([selectedCalorieText]);
    }
  }

  durationHourCustomize(String hours) {
    state.inputHours = invalidHourPartValue(hours) ? '' : hours;

    bool isValid = validDurationValue(state.hMSValue);
    if (isValid) {
      state.selectedDuration = state.hMSValue;
    }
    state.showDurationWarning = !isValid;
    state.canSaveDuration = isValid;
    update(['canSaveDuration']);
  }

  durationMinuteCustomize(String minutes) {
    state.inputMinutes = invalidMSPartValue(minutes) ? '' : minutes;

    bool isValid = validDurationValue(state.hMSValue);
    if (isValid) {
      state.selectedDuration = state.hMSValue;
    }
    state.showDurationWarning = !isValid;
    state.canSaveDuration = isValid;
    update(['canSaveDuration']);
  }

  durationSecondCustomize(String seconds) {
    state.inputSeconds = invalidMSPartValue(seconds) ? '' : seconds;

    bool isValid = validDurationValue(state.hMSValue);
    if (isValid) {
      state.selectedDuration = state.hMSValue;
    }
    state.showDurationWarning = !isValid;
    state.canSaveDuration = isValid;
    update(['canSaveDuration']);
  }

  /// 检查输入的小时是否合法
  bool invalidHourPartValue(String value) {
    return value.isEmpty || (value.isNotEmpty && int.parse(value) > 24);
  }

  /// 检查输入的分秒是否合法
  bool invalidMSPartValue(String value) {
    return value.isEmpty || (value.isNotEmpty && int.parse(value) > 59);
  }

  /// 时长大于1分钟并且小于3小时才是合法时长
  bool validDurationValue(String hms) {
    int sec = DateTool.hMS2Seconds(hms);
    return sec >= 60 && sec <= 180 * 60;
  }

  ///
  bool validDistanceValue() {
    return (state.distancePartOne.isNotEmpty && int.parse(state.distancePartOne) > 0) ||
        (state.distancePartTwo.isNotEmpty && int.parse(state.distancePartTwo) > 0);
  }

  distancePartTwoCustomize(String value) {
    state.distancePartTwo = value;

    bool isValid = validDistanceValue();
    if (isValid) {
      state.selectedDistance = '${state.distancePartOne.isEmpty ? 0 : state.distancePartOne}.${value.isEmpty ? 0 : value}';
    }

    state.showDurationWarning = !isValid;
    state.canSaveDistance = isValid;
    update(['canSaveDistance']);
  }

  distancePartOneCustomize(String value) {
    state.distancePartOne = value;

    bool isValid = validDistanceValue();
    if (isValid) {
      state.selectedDistance = '${value.isEmpty ? 0 : value}.${state.distancePartTwo.isEmpty ? 0 : state.distancePartTwo}';
    }

    state.showDurationWarning = !isValid;
    state.canSaveDistance = isValid;
    update(['canSaveDistance']);
  }
}
